package org.sunshine.mamadu;

import java.util.ArrayList;
import java.util.List;
import java.util.Collections;

public class MamaduParser {

    private static final String AD_START = "<tr><td align=\"left\" bgcolor=\"#E2DCBA\" colspan=\"2\"><b>";
    private static final String AD_END = "<tr><td colspan=\"2\" height=\"6\"></td></tr>";

    private static final String NAME_END = "</b>";
    private static final String MAIL_START = "mailto:";
    private static final String MAIL_END = "\">";

    private static final String PHONE_START = "тел. <b>";
    private static final String PHONE_END = "</b><br>";

    private static final String MESSAGE_START = "<tr><td align=\"left\" colspan=\"2\" width=\"770\" bgcolor=\"#E2DCBA\">";
    private static final String MESSAGE_END = "</td></tr>";

    private static final String DATE_START = "<tr bgcolor=\"#E2DCBA\"><td align=\"right\" colspan=2><font size=\"1\">";
    private static final String DATE_END = "&nbsp;&nbsp;&nbsp;</font>";


    private final String DETAILS_SECTION_START = ".<a class";

    public List<Advertisement> parse(String source) throws Exception {
        LocalLogger.LOGGER.fine("parsing source html...");
        List<String> list = new ArrayList<String>();
        addNotParsedAdvertisement(source, 0, list);
        return getAdvertisments(list);
    }

    private List<Advertisement> getAdvertisments(List<String> list) {
        ArrayList<Advertisement> result = new ArrayList<Advertisement>(list.size());
        for (String content : list) {
            Advertisement advertisement = new Advertisement();

            int startIndex = 0;
            int name_end_index = content.indexOf(NAME_END, startIndex);
            if (name_end_index != -1) {
                String name = content.substring(0, name_end_index);
                advertisement.setName(name);
                startIndex = name_end_index + NAME_END.length();
            }

            int mail_start_index = content.indexOf(MAIL_START, startIndex);
            if (mail_start_index != -1) {
                mail_start_index += MAIL_START.length();
                int end_index = content.indexOf(MAIL_END, mail_start_index);
                String email = content.substring(mail_start_index, end_index);
                advertisement.setEmail(email);
                startIndex = end_index + MAIL_END.length();
            }


            int phone_start_index = content.indexOf(PHONE_START, startIndex);
            if (phone_start_index != -1) {
                phone_start_index += PHONE_START.length();
                int end_index = content.indexOf(PHONE_END, phone_start_index);
                String phone = content.substring(phone_start_index, end_index);
                advertisement.setPhone(phone);
                startIndex = end_index + MAIL_END.length();
            }


            int message_start_index = content.indexOf(MESSAGE_START, startIndex);
            if (message_start_index == -1) {
                throw new IllegalStateException("Cannot get content of the advetisement #" + result.size());
            }
            message_start_index += MESSAGE_START.length();
            int message_end_index = content.indexOf(MESSAGE_END, message_start_index);
            String message = content.substring(message_start_index, message_end_index);


            int detailsSectionStartIndex = message.indexOf(DETAILS_SECTION_START);
            if (detailsSectionStartIndex != -1){
                message = message.substring(0, detailsSectionStartIndex);
            }

            message = XmlUtils.escape(message);
            advertisement.setContent(message);

            startIndex = message_end_index + MESSAGE_END.length();

            int date_start_index = content.indexOf(DATE_START, startIndex);
            if (date_start_index != -1) {
                date_start_index += DATE_START.length();
                int end_index = content.indexOf(DATE_END, date_start_index);
                String date = content.substring(date_start_index, end_index);
                advertisement.setDate(date);
            }

            result.add(advertisement);

        }
        Collections.reverse(result);
        return result;
    }

    private void addNotParsedAdvertisement(String source, int startIndex, List<String> list) {
        if (source.indexOf(AD_START, startIndex) == -1) return;
        int ad_start = source.indexOf(AD_START, startIndex) + AD_START.length();
        int ad_end = source.indexOf(AD_END, ad_start);

        String ad = source.substring(ad_start, ad_end);
        list.add(ad);
        addNotParsedAdvertisement(source, ad_end + AD_END.length(), list);
    }

}
